package com.yiwenup.leetcode.site;

import java.util.HashSet;
import java.util.Set;

/**
 * https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
 */
public class No003 {

    public int lengthOfLongestSubstring1(String s) {
        if (s.length() <= 1) {
            return s.length();
        }

        int res = 0;
        Set<Character> set = new HashSet<>();
        for (int i = 0; i < s.length(); i++) {
            boolean flag = true;
            set.add(s.charAt(i));
            for (int j = i + 1; j < s.length(); j++) {
                if (set.contains(s.charAt(j))) {
                    res = Math.max(j - i, res);
                    flag = false;
                    break;
                } else {
                    set.add(s.charAt(j));
                }
            }
            if (flag) {
                res = Math.max(res, s.length() - i);
            }
            set.clear();
        }

        return res;
    }
}
